package cn.sfrog.project.biz.admin.controller;

import cn.sfrog.common.annotation.Log;
import cn.sfrog.common.annotation.RepeatSubmit;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.AjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.enums.BusinessType;
import cn.sfrog.common.utils.poi.ExcelUtil;
import cn.sfrog.project.biz.admin.domain.vo.*;
import cn.sfrog.project.biz.admin.mapper.BizServiceUserMapper;
import cn.sfrog.project.biz.admin.service.IBizServiceService;
import cn.sfrog.project.biz.admin.service.IBizServiceUserService;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.domain.vo.app.AppServiceResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Api(tags = "管理后台-服务相关")
@RestController
@RequestMapping("/biz/service")
public class BizServiceController extends BaseController {
    @Resource
    private IBizServiceService bizServiceService;
    @Autowired
    private IBizServiceUserService bizServiceUserService;


    /**
     * 查询服务列表
     */
    @ApiOperation("查询列表")
    @PreAuthorize("@ss.hasPermi('biz:service:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizServiceQueryVO queryVO) {
        startPage();
        List<BizServiceVO> list = bizServiceService.selectBizServiceList(queryVO);
        return getDataTable(list);
    }

    @ApiOperation("查询服务列表")
    @GetMapping("/getServiceList")
    public AjaxResult getServiceList(BizServiceQueryVO queryVO) {
        startPage();
        List<BizServiceVO> list = bizServiceService.selectBizServiceList(queryVO);
        return AjaxResult.success(list);
    }

    /**
     * 导出服务列表
     */
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('biz:service:export')")
    @Log(title = "服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizServiceQueryVO query) {
        List<BizServiceVO> list = bizServiceService.selectBizServiceList(query);
        ExcelUtil<BizServiceVO> util = new ExcelUtil<BizServiceVO>(BizServiceVO.class);
        util.exportExcel(response, list, "服务数据");
    }

    /**
     * 获取服务详细信息
     */
    @ApiOperation("获取详情")
    @PreAuthorize("@ss.hasPermi('biz:service:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bizServiceService.selectBizServiceById(id));
    }

    /**
     * 新增服务
     */
    @ApiOperation("新增")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:service:add')")
    @Log(title = "服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizServiceFormVO form) {
        return toAjax(bizServiceService.insertBizService(form));
    }

    /**
     * 修改服务
     */
    @ApiOperation("修改")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:service:edit')")
    @Log(title = "服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizServiceFormVO form) {
        return toAjax(bizServiceService.updateBizService(form));
    }

    /**
     * 删除服务
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('biz:service:remove')")
    @Log(title = "服务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizServiceService.deleteBizServiceByIds(ids));
    }

    /**
     * 更改状态服务
     */
    @ApiOperation("更改状态")
    @PreAuthorize("@ss.hasAnyPermi('biz:service:changeStatus,biz:service:changeStatusBatch')")
    @Log(title = "服务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody @Valid BizStatusChangeVO vo) {
        return AjaxResult.success(bizServiceService.changeStatus(vo));
    }

    @ApiOperation("关联人员")
    @PreAuthorize("@ss.hasPermi('biz:service:relationUser')")
    @Log(title = "服务", businessType = BusinessType.INSERT)
    @PostMapping("/relationUser")
    public AjaxResult relationUser(@RequestBody @Validated BizServiceRelationUserFormVO form){
        return AjaxResult.success(bizServiceUserService.relationUser(form));
    }

    @ApiOperation("查询关联人员")
    @GetMapping("/getRelationUserIdList")
    public AjaxResult getRelationUserIdList(BizServiceRelationUserQueryVO queryVO){
        return AjaxResult.success(bizServiceUserService.selectRelationUserIdList(queryVO));
    }

    @ApiOperation("查询服务人员的服务或套餐列表")
    @GetMapping("/getProductList")
    public AjaxResult getProductList(BizProductQueryVO queryVO){
        List<AppServiceResultVO> resultVOList = bizServiceUserService.selectProductList(queryVO);
        return AjaxResult.success(resultVOList);
    }
}
